package cn.cggc.expl.demo.controller;

import cn.cggc.admin.entity.R;
import cn.cggc.admin.service.user.UserService;
import cn.cggc.admin.utils.DesUtils;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserService userService;

    @Value("${expl.system.id}")
    String explSysId;


    /***
     * 用户登录调用示例
     * */
    @GetMapping("/test/login")
    @ResponseBody
    public R login(){
        try{
            logger.info("demo->调用登录接口");
            String password = "123456";
            password = DesUtils.encryption(explSysId,password);
            return userService.login("xujian", password);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 通过token获取用户信息示例
     * */
    @GetMapping("/test/user/token")
    @ResponseBody
    public R getUserInfoByToken(){
        try{
            logger.info("demo->获取用户信息");
            String token = "21e2bff0bee70ce2aca57540b679486f";
            token = DesUtils.encryption(explSysId,token);
            return userService.getUserInfoByToken(token);
        }catch (Exception e){
            return R.error();
        }
    }


    /**
     * 通过userId取得用户信息
     * */
    @GetMapping("/test/user/uid")
    @ResponseBody
    public R getUserInfoByUserId(){
        try{
            long uid = 138;
            return userService.getUserInfoById(uid);
        }catch (Exception e){
            return R.error();
        }
    }


    /**
     * 用户主动下线，使token过期
     * */
    @GetMapping("/test/user/logout")
    @ResponseBody
    public R userLogout(){
        try{
            String token = "21e2bff0bee70ce2aca57540b679486f";
            token = DesUtils.encryption(explSysId,token);
            return userService.logout(token);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 取得全体组织结构，根据需求调用
     * */
    @GetMapping("/test/org/total")
    @ResponseBody
    public R orgTotal(){
        try{
            String token = "96a55faa1a585176c6e6955665b8d645";
            token = DesUtils.encryption(explSysId,token);
            return userService.getOrgTotal(token);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 获取用户所在部门
     * */
    @GetMapping("/test/depart/mine")
    @ResponseBody
    public R departMine(){
        try{
            String token = "96a55faa1a585176c6e6955665b8d645";
            token = DesUtils.encryption(explSysId,token);
            return userService.getDepartMine(token);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 取得我所在的组织架构树
     * */
    @GetMapping("/test/org/mine")
    @ResponseBody
    public R orgMine(){
        try{
            String token = "b00a3eba58613d0e4f9b2e6c1ba31155";
            token = DesUtils.encryption(explSysId,token);
            return userService.getOrgMine(token);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 取得用户所在的组织架构树，包含用户信息
     * */
    @GetMapping("/test/org/mineByUser")
    @ResponseBody
    public R orgMineByuser(){
        try{
            String token = "ca26084063809131ca0664f7609baa53";
            token = DesUtils.encryption(explSysId,token);
            return userService.getOrgMineByUser(token);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 取得指定部门所在的组织架构树
     * */
    @GetMapping("/test/org/byDeptId")
    @ResponseBody
    public R orgByDeptId(){
        try{
            long deptId = -1;
            //-1 or null表示取整体架构
            return userService.getOrgByDeptId(deptId);
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 取得某个部门的用户信息
     * */
    @GetMapping("/test/depart/user")
    @ResponseBody
    public R usersDepart(){
        try{
            String token = "906bc63d39a1462a497a8d04fba26ffc";
            token = DesUtils.encryption(explSysId,token);
            return userService.usersDepart(token,18l);
        }catch (Exception e){
            return R.error();
        }
    }
    /**
     * 用户修改密码
     * */
    @GetMapping("/test/user/changePassword")
    @ResponseBody
    public R userChangePassword(){
        try{
            String username = "xujian";
            String oldPassword = "123456";
            oldPassword = DesUtils.encryption(explSysId,oldPassword);
            String newPassword = "123";
            newPassword = DesUtils.encryption(explSysId,newPassword);
            return userService.userChangePassword(username,oldPassword,newPassword);
        }catch (Exception e){
            return R.error();
        }
    }
    /**
     * 查询用户信息，提供用户信息的updated时间和created时间大于某个时间进行筛选
     * */
    @GetMapping("/test/user/getByTime")
    @ResponseBody
    public R getUserByTime(){
        try{
            //null or "" 表示全部查询
            String gmtCreate = "2017-08-15 21:40:39";
            String gmtModified = "";
            return userService.getUserByTime(gmtCreate,gmtModified);
        }catch (Exception e){
            return R.error();
        }
    }
    /**
     * 查询所有用户,支持模糊搜索
     * */
    @GetMapping("/test/user/getSelected")
    @ResponseBody
    public R getUserSelected(){
        try{
            //null or "" 表示全部查询
            String mobile = "17699999999";
            long deptId = 0;
            String name = "";
            String idCard = "";
            String email = "";
            //分页,page 页，size 数量
            int page=1;
            int size=10;
            return userService.getUserSelected(mobile,deptId,name,idCard,email,page,size);
        }catch (Exception e){
            return R.error();
        }

    }
    /**
     * 根据部门ID查询部门
     * */
    @GetMapping("/test/dept/getByDeptId")
    @ResponseBody
    public R getDeptByDeptId(){
        try{
            long deptId = 18;
            return userService.getByDeptId(deptId);
        }catch (Exception e){
            return R.error();
        }

    }
}
